package com.opennetexam.manager.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Description TODO
 * @author 奶茶
 * Created by on 2018/10/23 19:06.
 */
@Repository
@ConditionalOnProperty(name = "TestMode",havingValue = "2")
public class RedisDao {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    private ValueOperations<Object,Object> valueOperations;
    private ListOperations<Object,Object> listOperations;
    private HashOperations hashOperations;

    @PostConstruct
    public void getValueOperation(){
        valueOperations = redisTemplate.opsForValue();
        listOperations = redisTemplate.opsForList();
        hashOperations = redisTemplate.opsForHash();
    }

    public void setValue(String key, String value){
        valueOperations.set(key, value);
    }

    public void setValue(String key, String value,Integer timeout){
        valueOperations.set(key,value,timeout, TimeUnit.SECONDS);
    }

    public String getValue(String key){
        return (String) valueOperations.get(key);
    }

    public void addList(String key, String value){
        listOperations.leftPush(key, value);
    }
    public Long getListSize(String key){
        return listOperations.size(key);
    }

    public void setHashMap(String key, Map map){
        hashOperations.putAll(key, map);
    }
    public Map getHashMap(String key){
        return hashOperations.entries(key);
    }

    public void delete(String key){
        redisTemplate.delete(key);
    }

}
